//
//  USTalkAnswerCell.m
//  us
//
//  Created by lhx on 2025/5/23.
//

#import "USTalkAnswerCell.h"

@interface USTalkAnswerCell ()

@property(nonatomic,strong) UILabel *nameLab;
@property(nonatomic,strong) UILabel *timeLab;
@property(nonatomic,strong) UILabel *contentLab;
@property (nonatomic, strong) UIButton *editAnswerBtn;

@property (nonatomic, strong) UIView *remindView;
@property (nonatomic, strong) UILabel *remindLab;
@property (nonatomic, strong) UIButton *remindBtn;

@property (nonatomic, strong) UILabel *unReadLab;

@property (nonatomic, strong) UIView *lineView;

@property (nonatomic, strong) MASConstraint *contentConstraint;
@property (nonatomic, strong) MASConstraint *remindConstraint;
@property (nonatomic, strong) MASConstraint *unreadConstraint;

@end

@implementation USTalkAnswerCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setupView];
    }
    return self;
}

- (void)setModel:(USCommentModel *)model {
    _model = model;
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:model.gmtModified];
    NSString *dayStr = [date jk_stringWithFormat:@"yyyy-MM-dd HH:mm:ss"];
    self.timeLab.text = dayStr;
    self.contentLab.text = model.content;
    
    BOOL isMy = [model.userId isEqualToString: USUser.user.userId];
    BOOL isExist = model.dailyTalkCommentId.length > 0;
    if (isMy) {
        self.nameLab.text = USUser.user.nickname;
        
        self.remindLab.text = @"要写点什么呢";
        [self.remindBtn setTitle:@"填写答案" forState:0];
        [self.remindBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(96);
        }];
    }else{
        self.nameLab.text = USUser.user.relUser.nickname;
        self.remindLab.text = @"对方还没有回答";
        [self.remindBtn setTitle:@"提醒ta" forState:0];
        [self.remindBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(83);
        }];
    }
    
    self.editAnswerBtn.hidden = !isMy || !isExist;
    self.lineView.hidden = isMy;
    
    if (isExist) {
        self.timeLab.hidden = NO;
        self.remindView.hidden = YES;
        
        [self.remindConstraint uninstall];
        if (!isMy && model.noCompletion) {
            //我未搭，对方答了，则对方内容不可见
            
            self.contentLab.hidden = YES;
            self.unReadLab.hidden = NO;
            [self.contentConstraint uninstall];
            [self.unReadLab mas_updateConstraints:^(MASConstraintMaker *make) {
                self.unreadConstraint = make.bottom.mas_equalTo(-28);
            }];
        }else {
            self.contentLab.hidden = NO;
            self.unReadLab.hidden = YES;
            [self.unreadConstraint uninstall];
            [self.contentLab mas_updateConstraints:^(MASConstraintMaker *make) {
                self.contentConstraint = make.bottom.mas_equalTo(-16);
            }];
        }
        
    }else {
        self.timeLab.hidden = YES;
        self.contentLab.hidden = YES;
        self.unReadLab.hidden = YES;
        self.remindView.hidden = NO;
        
        [self.contentConstraint uninstall];
        [self.unreadConstraint uninstall];
        [self.remindView mas_updateConstraints:^(MASConstraintMaker *make) {
            self.remindConstraint = make.bottom.mas_equalTo(0);
        }];
    }
    
}

- (void)editAnswerAction {
    if (self.editAnswerBlock) {
        self.editAnswerBlock();
    }
}
- (void)remindAction {
    if([self.model.userId isEqualToString: USUser.user.userId]){
        if (self.answerBlock) {
            self.answerBlock();
        }
    }else {
        @weakify(self)
        
        [self.remindBtn us_handleRemindTap:self.remindBtn];
        if (self.remindBlock) {
            self.remindBlock();
        }
    }
}

- (void)setupView{
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.backgroundColor = UIColor.clearColor;
    self.contentView.backgroundColor = UIColor.clearColor;
    
    self.nameLab = [UILabel new];
    self.nameLab.font = [UIFont systemFontOfSize:16];
    self.nameLab.textColor = UIColor.labelColor;
    self.nameLab.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.nameLab];
    [self.nameLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(24);
        make.right.mas_offset(-100);
        make.top.mas_offset(12);
        make.height.mas_equalTo(18);
    }];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"comment_edit"] forState:0];
    self.editAnswerBtn = btn;
    [btn addTarget:self action:@selector(editAnswerAction) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:btn];
    [btn mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-24);
        make.centerY.equalTo(self.nameLab).offset(0);
    }];
    
    self.timeLab = [UILabel new];
    self.timeLab.font = [UIFont systemFontOfSize:12];
    self.timeLab.textColor = [UIColor jk_colorWithHex:0x212121 andAlpha:0.2];
    self.timeLab.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.timeLab];
    [self.timeLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLab);
        make.top.mas_equalTo(self.nameLab.mas_bottom).offset(4);
        make.height.mas_equalTo(12);
    }];
    
    self.contentLab = [UILabel new];
    self.contentLab.font = [UIFont systemFontOfSize:16];
    self.contentLab.textColor = UIColor.labelColor;
    self.contentLab.textAlignment = NSTextAlignmentLeft;
    self.contentLab.numberOfLines = 0;
    [self.contentView addSubview:self.contentLab];
    [self.contentLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLab);
        make.right.mas_offset(-24);
        make.top.mas_equalTo(self.timeLab.mas_bottom).offset(16);
        self.contentConstraint = make.bottom.mas_equalTo(-16);
    }];
    
    [self.contentView addSubview:self.remindView];
    [self.remindView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(120);
        self.remindConstraint = make.bottom.mas_equalTo(0);
    }];
    
    [self.contentView addSubview:self.unReadLab];
    self.unReadLab.hidden = YES;
    [self.unReadLab mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(62);
        make.left.right.mas_equalTo(0);
        make.height.mas_equalTo(60);
        self.unreadConstraint = make.bottom.mas_equalTo(-28);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = [UIColor mi_3_color];
    line.hidden = YES;
    self.lineView = line;
    [self.contentView addSubview:line];
    [line mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(24);
        make.right.mas_offset(-24);
        make.height.mas_equalTo(1);
        make.bottom.mas_equalTo(0);
    }];
    
    
}

- (UILabel *)unReadLab {
    if (_unReadLab == nil) {
        _unReadLab = [[UILabel alloc]init];
        _unReadLab.text = @"对方的答案要填写后才能查看哦";
        _unReadLab.font = [UIFont systemFontOfSize:14];
        _unReadLab.textColor = [UIColor jk_colorWithHex:0x212121 andAlpha:0.6];
        _unReadLab.textAlignment = NSTextAlignmentCenter;
    }
    return _unReadLab;
}
- (UIView *)remindView {
    if (_remindView == nil) {
        
        _remindView = [[UIView alloc]init];
        
        UILabel *lab = [UILabel new];
        lab.text = @"对方还没有回答";
        lab.font = [UIFont systemFontOfSize:14];
        lab.textColor = [UIColor jk_colorWithHex:0x212121 andAlpha:0.6];
        lab.textAlignment = NSTextAlignmentCenter;
        self.remindLab = lab;
        [_remindView addSubview:lab];
        [lab mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.top.mas_offset(16);
            make.height.mas_equalTo(16);
        }];
        
        UIButton *remindBtn = [UIButton us_remindButtonWithTitle:@"提醒ta"];
        remindBtn.layer.cornerRadius = 14;
        self.remindBtn = remindBtn;
        [_remindView addSubview:remindBtn];
        [remindBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lab.mas_bottom).offset(18);
            make.centerX.mas_equalTo(0);
            make.width.mas_equalTo(83);
            make.height.mas_equalTo(28);
        }];
        
     
        [remindBtn addTarget:self action:@selector(remindAction) forControlEvents:UIControlEventTouchUpInside];
   
    }
    return _remindView;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
